﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Dto.Report;
using ServiceCore.Entity.Model;
using ServiceCore.Entity.View;
using SqlSugar;

namespace ServiceCore.Service
{
    /// <summary>
    /// 数据服务
    /// </summary>
    public partial class ReportService : SqlsugarServices<RecordInfo>
    {
        /// <summary>
        /// 标签统计参数校验
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<RecordInfo, List<TagSumDto>> Tag_Report_Verify(BaseParam<RecordInfo, List<TagSumDto>> arg)
        {
            if (arg == null)
                return new BaseParam<RecordInfo, List<TagSumDto>>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<RecordInfo, List<TagSumDto>>("参数不能为空");
            if (arg.Param.SiteId == 0)
                return new BaseParam<RecordInfo, List<TagSumDto>>("分组id不能为空");            

            return arg;
        }

        /// <summary>
        /// 标签统计记录 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public void Tag_Report_Record(BaseParam<RecordInfo, List<TagSumDto>> arg)
        {
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return;

            arg.Data = this.Context.Queryable<V_Record>()
                .Where(x => x.SiteId == arg.Param.SiteId)
                .GroupBy(x => new { x.TagId, x.TagName, x.TagtypeName })
                .OrderBy(x => x.TagId)
                .Select(x => new TagSumDto() 
                {
                    TagId = x.TagId,
                    TagName = x.TagName,
                    TagTypeName = x.TagtypeName,
                    Total = SqlFunc.AggregateSum(x.DataValue)
                })
                .ToList();
            arg.Param = null;
        }

        /// <summary>
        /// 标签统计颜色
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public void Tag_Report_Color(BaseParam<RecordInfo, List<TagSumDto>> arg)
        {
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return;
            if (arg.Data.Any() == false)
                return;
            arg.Data.ForEach(x => x.ClassName = "positive");

            var lstThreshold = this.Context.Queryable<ThresholdInfo>().Where(x => x.StartValue != 0).ToList();
            if (lstThreshold.Any() == false)
                return;

            bool isWarn = false;
            foreach (var tag in arg.Data)
            {
                isWarn = false;
                foreach (var threshold in lstThreshold)
                {
                    if (tag.Total >= threshold.StartValue && tag.Total <= threshold.EndValue)
                    { 
                        isWarn = true;
                        break;
                    }
                }

                if (isWarn)
                    tag.ClassName = "highlight";
            }
        }
    }
}
